package yl.hs.bmipfull.controllers.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.system.IDataDictService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

/**
 * DataDictTypeController 数据字典类型接口控制器
 *
 * @author 谢景广
 * @category 系统管理——字典类型
 */
@Api(value = "数据字典类型信息Web接口", tags = "系统管理——字典类型")
@RequestMapping(path = "/api/dict/type")
@RestController
public class DataDictTypeController extends ControllerBase {

    @Autowired
    IDataDictService dtSvc;

    @ApiOperation(value = "查询数据字典类型", notes = "查询数据字典类型")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", dtSvc.queryList(pageQuery));
        } catch (Exception e) {
            return error(e);
        }
    }

    @ApiOperation(value = "保存字典类型", notes = "保存编辑或者新建的字典类型")
    @RequestMapping(path = "", method = RequestMethod.PUT)
    @ResponseBody
    public WebResult save(@RequestBody SubmitModel model) {
        try {
            var result = dtSvc.saveDataType(model);
            if (result > 0) {
                return success("ok");
            }
            return fail("信息保存失败");
        } catch (Exception ex) {
            return error(ex);
        }
    }

    @ApiOperation(value = "查询字典类型", notes = "查询字典类型编号获取字典类型")
    @RequestMapping(path = "/{sn}", method = RequestMethod.GET)
    @ResponseBody
    public WebResult find(@ApiParam(value = "数据编号", required = true) @PathVariable("sn") String sn) {
        try {
            var type = dtSvc.findDictTypeBySn(sn);
            if (type != null) {
                return success("ok", type);
            } else
                return notfound(String.format("没有找到编号为%s的数据字典类型", sn));
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "删除字典类型信息", notes = "根据字典类型编号删除字典类型信息")
    @RequestMapping(path = "", method = RequestMethod.DELETE)
    @ResponseBody
    public WebResult delete(@ApiParam(value = "数据编号", name = "sn", allowMultiple = true, required = true) @RequestParam("sn") String sn) {
        try {
            var result = dtSvc.deleteDataType(sn.split(","));
            if (result > 0)
                return success("ok");
            else
                return fail("没有删除任何数据");
        } catch (Exception ee) {
            return error(ee);
        }
    }

    @ApiOperation(value = "验证数据字典类型编码是否已经存在", notes = "验证数据字典类型编码是否已经存在")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public WebResult checkTypeCode(@ApiParam(value = "数据编号", name = "sn") @RequestParam("sn") String sn,
                                   @ApiParam(value = "类型编码", name = "typeCode") @RequestParam("typeCode") String typeCode) {
        Boolean aBoolean = dtSvc.checkTypeCodeExists(typeCode, sn);
        return success("验证结果", aBoolean);
    }
}
